/**
 * @author Linrui
 * @since 2011-05-06
 * 产品业务层结构实现类
 */
package com.qeado.product.service.implement;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import jeasy.analysis.MMAnalyzer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qeado.product.dao.ProductDao;
import com.qeado.product.model.Product;
import com.qeado.product.service.ProductService;
import com.qeado.util.FileUpload;
import com.qeado.util.KeyAnalyer;
import com.qeado.util.PageHelper;
@Service
public class ProductServiceImplement implements ProductService {

	@Autowired
	private ProductDao productDao;
	
	public PageHelper listBySort(int pageNum, int pageSize, String typeCode,String orderby, String asc ,String queryType) {
		if( pageSize == 10 || pageSize == 20 || pageSize == 50 || pageSize == 100) {
			return productDao.listBySort(pageNum, pageSize, typeCode, orderby, asc, queryType);
		} else{
			System.out.println("记录大小参数不符合标准");
			return null;
		}
	}

	public PageHelper listBySort_firstLevel(int pageNum, int pageSize,
			String typeCode, String orderBy, String asd, String queryType) {
		
		return null;
	}


	public PageHelper listByUser(int pageNum, int pageSize, String userName,
			String orderby, String asc, String queryType) {
		if( pageSize == 10 || pageSize == 20 || pageSize == 50 || pageSize == 100) {	
			if( ( !"".equals(userName) && userName!=null ) || ( !"".equals(orderby) && orderby!=null ) || 
				( !"".equals(asc) && asc!=null ) || ( !"".equals(queryType) && queryType!=null ) ) {
				return productDao.listByUser(pageNum, pageSize, userName, orderby, asc, queryType);
			} else 
				return null;			
		} else
			return null;
	}

	public boolean insertProduct(Product product) {
		product.setId(UUID.randomUUID().toString());
		product.setActiveFlag("1");
		product.setProdLevel("1");
		product.setPageViews(0);
		return productDao.insertProduct(product);
	}

	public boolean insertProduct(Product product, FileUpload upload) {
		if( upload == null){
			product.setFileName("default/default.gif");
			return insertProduct(product);
		}
		else {
			upload.upload();
			product.setFileName(upload.getImageFileName());
			return insertProduct(product);
		}
	}

	public boolean deleteProductById(String id) {
		return productDao.deleteProduct(id);
	}

	public Product getProductById(String id) {
		return productDao.getProdById(id);
	}

	public boolean updateProduct(Product product) {
		
		return productDao.updateProduct(product);
	}

	public PageHelper listByKey(int pageNum, int pageSize, String key,
			String orderBy, String asc) {
//		try {
//			key = KeyAnalyer.getInstance().getMMAnalyzer().segment(key, ",");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		String[] input = key.split(",");
//		List<String> keyList = Arrays.asList(input);
		key = key.trim();
		key = "%" + key + "%";
		return productDao.listByKey(pageNum, pageSize, key, orderBy, asc );
	}

	public PageHelper listAll(int pageNum, int pageSize, String orderBy,
			String asc, String queryType) {
//		if( "0".equals(queryType) || queryType == null)
//			queryType = "%";
		return productDao.listAll(pageNum, pageSize, orderBy, asc, queryType);
	}

	public PageHelper listBySortLike(int pageNum, int pageSize,
			String typeCode, String orderBy, String asc, String queryType) throws Exception{
		if( pageNum <= 0 || pageSize < 0)
			return null;
		if ( !( pageSize == 10 || pageSize == 20 || pageSize == 50 || pageSize == 100) ){
			return null;
		}
		typeCode += "%";
		return productDao.listBySortLike(pageNum, pageSize, typeCode, orderBy, asc, queryType);
	}

}
